package com.stylefeng.guns.rest.modular.auth.util;

import java.math.BigInteger;
import java.security.MessageDigest;

public class MD5Utils {

    /***
     * MD5加码 生成32位md5码
     */
    public static String string2MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();

    }

    /**
     * 加密解密算法 执行一次加密，两次解密
     */
    public static String convertMD5(String inStr) {

        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;

    }

    public static String md5(String str) {
        try {
            // ⽣成⼀个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            /* digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16
             * 位的hex值，实际上就是8位的字符
             * BigInteger函数则将8位的字符串转换成16位hex值，⽤字符串来表示；得到字符
             * 串形式的hash值
             */
            String md5 = new BigInteger(1, md.digest()).toString(16);
            StringBuilder sb = new StringBuilder();
            //BigInteger会把0省略掉，需补全⾄32位
            for (int i = md5.length(); i < 32; i++) {
                sb.append("0");
            }
            sb.append(md5);
            return sb.toString();
        } catch (Exception e) {
            throw new RuntimeException("MD5加密错误:" + e.getMessage(), e);
        }
    }

    //提交支付sign签名算法(sign = md5(appid + money + transp + key))
    public static String submitPaySign(String appid,String money,String transp,String key){
        String sign=appid+money+transp+key;
        return md5(sign);
    }
    //代付接口签名算法 md5(accountname + appid+ orderid + type)
    private static String getSigndaw(String accountname ,String appid,String idnumber,String key,String money,String orderid,String type){
        String sign=accountname+appid+idnumber+key+money+ orderid + type;
        return md5(sign);
    }



    // 测试主函数
    public static void main(String args[]) {
        String appId="4789";
        String money="1000";
        String transp="order_001";
        String key="834607fc284346dd9c5fd13ea66fc01b";
        String signjm = md5(appId+money+transp+key);
        System.out.println("===== ：" + signjm);
//        String s = new String("1111");
//        System.out.println("原始：" + s);
//        System.out.println("MD5后：" + string2MD5("4789100order_001834607fc284346dd9c5fd13ea66fc01b"));
//        System.out.println("加密的：" + convertMD5(s));
//        System.out.println("解密的：" + convertMD5("EFG@ABC"));

    }

}